<?php

namespace Modules\Buy\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Buy\Emails\RegisterMail;
use Modules\Buy\Http\Requests\AuthRequest;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Validator;
use Auth;
use Redirect;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Modules\Buy\Entities\Yonghu;
class RegisterController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */

//    前台注册
    public function zhu(){

        return view('dlzc.register');
    }

//    前台邮箱验证
    public function jia(Request $request,AuthRequest $authRequest)
    {
//        $post = $request->all();
//        dd($post);dadada
//            存数据,存数据:密码不能明文存吧,把密码加密存进去
        $post  = $request->all();
        $post["password"] = bcrypt($post["password"]);
        $post["email_token"] = str_random(10);
        $user = Yonghu::create($post);
//        可以加进去意思就是有返回值，那就可以发邮件
        \Mail::to($user)->send(new RegisterMail($user["email_token"]));

        return view('dlzc.jump')->with([
            'message'=>'邮件已经发送,请激活登录！',
            'url' =>'deng',
            'jumpTime'=>2,
            'name'=>'登录'
        ]);
//        return redirect('/deng')->with('邮件已经发送，请去激活再登录');
    }
//    配置激活邮箱路由
    public function jihuo($token)
    {
//        dd(123);
        $user = Yonghu::where("email_token",$token)->first();
//        判断这条数据是否存在,如果存在就执行以下操作
        if ($user){
            $user->status = 1;
            $user->save();
//            账号激活后自动绑用户登录
//            \Auth::login($user);

//            session()->flash("success","账号激活成功,热烈欢迎");
//            return redirect("/deng");
            return view("dlzc.jump")->with([
                'message'=>'账号激活成功,热烈欢迎！',
                'url' =>'deng',
                'jumpTime'=>2,
                'name'=>'登录'
            ]);

        }else{
//            session()->flash("danger","不好意思,未激活账号");
//            return redirect("/");
            return view("dlzc.jump")->with([
                'message'=>'不好意思,未激活账号！',
                'url' =>'deng',
                'jumpTime'=>2,
                'name'=>'登录'
            ]);
        }

    }

//    前台短信验证
    public function duanxin()
    {
        dd(123);
    }
}